<template>
  <div class='container-tags'>
    <el-card class="box">
      <el-row>
        <el-col :span="20">
          <el-form label-width="80px" :inline="true" size="small">
            <el-form-item label="标签名称">
              <el-input v-model="requestParams.tagName"></el-input>
            </el-form-item >
            <el-form-item label="状态">
              <el-select v-model="requestParams.state">
                <el-option :value="1" label="启用"></el-option>
                <el-option :value="0" label="禁用"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="clear()">清除</el-button>
              <el-button type="primary" @click="filter()">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="4" style="text-align:right">
          <el-button icon="el-icon-edit" type="success" @click="openDialog()" size="small">新增标签</el-button>
        </el-col>
      </el-row>
      <!-- 总条数 -->
      <el-alert
        :title="`数据一共 ${total} 条`"
        style="margin-bottom:15px"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      ></el-alert>
      <!-- 列表 -->
      <el-table :data="tags">
        <el-table-column label="序号" type="index" width="80px"></el-table-column>
        <el-table-column label="所属标签" prop="subjectName"></el-table-column>
        <el-table-column label="标签名称" prop="tagName"></el-table-column>
        <el-table-column label="创建者" prop="username"></el-table-column>
        <el-table-column label="创建日期">
          <template #default="{row}">{{row.addDate|parseTimeByString}}</template>
        </el-table-column>
        <el-table-column label="状态">
          <template #default="{row}">{{row.state===1?'已启用':'已禁用'}}</template>
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template #default="{row}">
            <el-button type="text" @click="toggleState(row)">{{row.state===1?'禁用':'启用'}}</el-button>
            <el-button type="text" @click="openDialog(row)" :disabled="row.state===1">修改</el-button>
            <el-button
            type="text"
            @click="delTag(row)"
            :disabled="row.state===1||row.totals
            > 0">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页栏 -->
    <el-pagination
        style="margin-top:20px;text-align:right"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :page-size="requestParams.pagesize"
        :current-page="requestParams.page"
        @current-change="pager"
        :page-sizes="[5,10,15,20]"
        @size-change="handleSizeChange"
        hide-on-single-page
      ></el-pagination>
    </el-card>
    <!-- 添加|修改 -->
    <tags-add ref="tagssAdd" :data="currTag" @updateList="updateList()"></tags-add>

  </div>
</template>

<script>
import { list, remove, changeState } from '@/api/hmmm/tags'
import TagsAdd from '../components/tags-add'

export default {
  name: 'PagesTags',
  components: { TagsAdd },
  data () {
    return {
      requestParams: {
        tagName: null,
        state: null,
        page: 1,
        pagesize: 10
      },
      total: 0,
      tags: [],
      currTag: {}
      // tag: {}
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 点击新增与修改弹框
    openDialog (tag = {}) {
      this.currTag = tag
      // 将回调延迟到下次 DOM 更新循环之后执行,在修改数据之后立即使用它，然后等待 DOM 更新
      // 回调的 this 自动绑定到调用它的实例上
      this.$nextTick(() => {
        this.$refs.tagssAdd.open()
      })
    },
    // 切换状态
    async toggleState (tag) {
      await changeState({
        // 传入一个参数
        id: tag.id,
        state: tag.state === 1 ? 0 : 1
      })
      // 提示框
      this.$message.success('操作成功')
      // 判断为1或者0并赋值
      tag.state = tag.state === 1 ? 0 : 1
    },
    // 删除
    async delTag (tag) {
      await this.$confirm('此操作将永久删除该标签, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      // 发送请求
      await remove(tag)
      // 提示框
      this.$message.success('删除成功')
      // 渲染
      this.getList()
    },
    // 搜索按钮
    filter () {
      this.requestParams.page = 1
      this.getList()
    },
    // 清除按钮
    clear () {
      this.requestParams = {
        tagName: null,
        state: null,
        page: 1,
        pagesize: 10
      }
    },
    // 渲染列表
    async getList () {
      const { data } = await list(this.requestParams)
      console.log(data)
      // 给空数组赋值
      this.tags = data.items
      // 给总条数赋值
      this.total = data.counts
    },
    // 切换条数
    handleSizeChange (size) {
      // 优化
      this.requestParams.page = 1
      // 给每页条数赋值
      this.requestParams.pagesize = size
      // 调用重新渲染列表数据
      this.getList()
    },
    // 切换分页
    pager (np) {
      this.requestParams.page = np
      this.getList()
    },
    // 新增|修改 后更新列表
    updateList () {
      if (!this.currTag.id) {
        this.requestParams.page = 1
      }
      this.getList()
    }
  }
}
</script>

<style scoped lang='less'>
.pagination{
  display: flex;
  justify-content: flex-end;
}
.tags-select{
  margin-right:10px;
}
.tagsBtn{
  display: flex;

  div{
display: inline-block;
  }
  .addBtn{
    display: flex;
    justify-content: flex-end;
    height: 32px;
  }
}

</style>
